function getNextVideo (container,ratio,previous,datafirst){
    previous = previous || false
    datafirst = datafirst || false
    
    var querystring = "action=nextVideoPalinsesto&ajax_next_video=true&prev="+previous;
    var width = getMaxWidth("palinsesto");
    var height = getMaxHeight(width,ratio);
    
    $.ajax({
        method: "get",
        url: location.href.substring(0,location.href.lastIndexOf('/')+1)+"wp-admin/admin-ajax.php",
        data: querystring, 
        success: function(response){
        if(datafirst == false)
            var data = getArray(response);
        else data = datafirst;
        if(data['breaking']=="1" || data['breaking'] == true || data['breaking'] == 1){
            
                 jwplayer(container).setup({
                  controlbar: "bottom",
                  provider:"http",
                  "http.startparam":"start",
                  width: width,
                  height: height,
                  autostart: true,
                  file: location.href.substring(0,location.href.lastIndexOf("/")+1)+'wp-content/plugins/video-wizard/libs/breakingnews/bn.mp4',
                  modes: [
                       {type: "flash", 
                        src: location.href.substring(0,location.href.lastIndexOf("/")+1)+"wp-content/plugins/video-wizard/libs/jwplayer/player.swf"
                        },
                       {type: "html5",
                        config:{provider:"video"}
                        },
                       {type:"download"}
                          ],
                  events:{
                  onComplete: function(){
                      jwplayer(container).setup({
                      flashplayer: location.href.substring(0,location.href.lastIndexOf('/')+1)+'wp-content/plugins/video-wizard/libs/jwplayer/player.swf',
                      file: data['file_position'],
                      controlbar: "bottom",
                      width: width,
                      height: height,
                      autostart: true,
                      provider:"http",
                      "http.startparam":"start",
                      plugins: {
                           "/wordpress/wp-content/plugins/video-wizard/js/breaking_news_plugin.js": {
                           }
                       },
                      events:{
                      onComplete: function(){getNextVideo(container,"16:9",data['id']);}}
                     });}
                    }
                    });
             }
        else{
            jwplayer(container).setup({
                  flashplayer: location.href.substring(0,location.href.lastIndexOf("/")+1)+'wp-content/plugins/video-wizard/libs/jwplayer/player.swf',
                  file: data['file_position'],
                  controlbar: "bottom",
                  width: width,
                  provider:"http",
                  "http.startparam":"start",
                  height: height,
                  autostart: true,
                  events:{
                  onComplete: function(){getNextVideo(container,"16:9",data['id']);}}
                 });
             }
             if(previous == false) return;
                $("#vidwiz-description").fadeOut("slow",function(){
                    $(this).html(data['description']);
                    $(this).fadeIn("slow");
                });
            }
        
    });
    
}

function getMaxWidth(id){
    var parent = $("#"+id).parent();
    return(parent.css('width'));
    }

function getMaxHeight(width,resolution){
    var ratio;
    switch(resolution){
        case "4:3":
            ratio = 1.33333;
            break;
        case "16:9":
            ratio = 1.778;
            break;
        default:
            ratio = 1.778;
            break;
    }
    width = width.replace("px","");
    width = parseInt(width);
    var height = width/ratio;
    height = Math.round(height);
    
    return height+"px";
    }

function showBox(nr){
    var boxToShow = $("#vidwiz-box"+nr);
    var boxToHide = getSelected();
    $(boxToHide).hide("slow",function(){
        $(boxToShow).show('slow',function(){
            $(this).attr('selected','selected');
        });
        $(this).removeAttr('selected');
    });
}

/**
 * 
 * @return l'id del container selezionato, false se nessun selezionato
 */
function getSelected(){
    var list = document.getElementsByName("vidwiz-box");
    var i;    var selected;
    if(list.length == 0){
        return false}
    for(i=0;i<list.length;i++){ 
        selected = list[i].getAttribute('selected');
        if(selected == 'selected'){
            return list[i];
        }
    }
    return false;
}